/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.system.domain.Departmentteacher;
import me.zhengjie.modules.system.domain.Teacher;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.UserRole;
import me.zhengjie.modules.system.mapper.TeacherMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.mapper.UserRoleMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.DepartmentteacherService;
import me.zhengjie.modules.system.domain.vo.DepartmentteacherQueryCriteria;
import me.zhengjie.modules.system.mapper.DepartmentteacherMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author shitou
* @date 2024-12-12
**/
@Service
@RequiredArgsConstructor
public class DepartmentteacherServiceImpl extends ServiceImpl<DepartmentteacherMapper, Departmentteacher> implements DepartmentteacherService {

    private final DepartmentteacherMapper departmentteacherMapper;

    private final UserRoleMapper userRoleMapper;

    private final TeacherMapper teacherMapper;

    private final UserMapper userMapper;

    @Override
    public PageResult<Departmentteacher> queryAll(DepartmentteacherQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(departmentteacherMapper.findAll(criteria, page));
    }

    @Override
    public List<Departmentteacher> queryAll(DepartmentteacherQueryCriteria criteria){


        return departmentteacherMapper.findAll(criteria);
    }

    public List<Departmentteacher> getDepartmentteacherList(DepartmentteacherQueryCriteria criteria){
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        String currentUserName = authentication.getName();
//        User currentUser = userMapper.findByUsername(currentUserName);
//
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("userId",currentUser.getId());
//        Teacher teacher = teacherMapper.selectOne(queryWrapper);
//
//        QueryWrapper queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.eq("teacherId",teacher.getId());
//        Departmentteacher departmentteacher = departmentteacherMapper.selectOne(queryWrapper1);
        criteria.setDepartmentId(criteria.getDepartmentId());
        return departmentteacherMapper.findAll(criteria);
    }

    public List<Departmentteacher> getCourseDepartmentteacherList(DepartmentteacherQueryCriteria criteria){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",currentUser.getId());
        Teacher teacher = teacherMapper.selectOne(queryWrapper);

        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("teacherId",teacher.getId());
        Departmentteacher departmentteacher = departmentteacherMapper.selectOne(queryWrapper1);
        criteria.setDepartmentId(departmentteacher.getDepartmentId());
        return departmentteacherMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Departmentteacher resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Departmentteacher resources) {
        Departmentteacher departmentteacher = getById(resources.getId());
        departmentteacher.copy(resources);
        saveOrUpdate(departmentteacher);
        Teacher teacher = teacherMapper.selectById(departmentteacher.getTeacherId());


        if(departmentteacher.getType() == 0){
            UserRole userRole = new UserRole();
            userRole.setUser_id(teacher.getUserid());
            userRole.setRole_id(5L);
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",teacher.getUserid());
            userRoleMapper.update(userRole,queryWrapper);
        } else {
            UserRole userRole = new UserRole();
            userRole.setUser_id(teacher.getUserid());
            userRole.setRole_id(6L);

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",teacher.getUserid());
            userRoleMapper.update(userRole,queryWrapper);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Departmentteacher> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Departmentteacher departmentteacher : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" departmentid",  departmentteacher.getDepartmentId());
            map.put(" teacherid",  departmentteacher.getTeacherId());
            map.put(" type",  departmentteacher.getType());
            map.put(" lastupdatetime",  departmentteacher.getLastupdatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}